<?php
/**
 * Created by PhpStorm.
 * User: chenx
 * Date: 18-3-10
 * Time: 下午9:36
 */

namespace backend\controllers;


use common\models\Active;
use common\models\ActivityAttr;

use common\models\Order;
use common\service\ActiveService;
use common\service\CommonService;
use common\service\WechatService;
use common\service\GlobalUrlService;
use common\service\ConstantService;


class ActiveController extends BaseController
{
    /**
     * 活动列表页
     * @return string
     */
    public function actionIndex(){
        if($this->isAjax() && $this->isGet()){
            $query = Active::find();
            $query->andWhere(['is_del'=>0]);
            if(!empty($this->get('active_name'))){
                $query->andWhere(['like','active_name',$this->get('active_name')]);
            }

            $query->andWhere(['type'=>$this->get('type')]);
            $p = intval( $this->get('page',1) );
            $this->page_size = intval( $this->get('limit',10) );
            $p = ( $p > 0 )? $p : 1;
            $count = $query->count();

            $offset = ($p - 1) * $this->page_size;

            $lists = $query->orderBy(['set_top' => SORT_DESC,'update_time' => SORT_DESC])
                ->offset( $offset )
                ->limit( $this->page_size )
                ->asArray()
                ->all();
            return $this->renderPageJson($lists,$count,ConstantService::$response_tab_success);
        }
        $type = $this->get('type');
        return $this->render('index',['type'=>$type]);
    }



    //查询活动详情
    public function actionActiveInfo(){
        $aid = $this->get('id');
        $active = Active::findOne(['id'=>$aid]);

        $activeAttr = ActiveService::GetAttrlist($aid);
        return  $this->render('active-info',['active_attr'=>$activeAttr,'active'=>$active]);
    }

    //活动二维码
    public function actionQrcode(){
        if ($this->isAjax() && $this->isPost()){
            $aid = $this->post('id');
            $active = Active::find()->where(['id'=>$aid])->one();
            if (empty($active['qrcode'])){
                $qrcode = WechatService::getWechatQrcode($aid,"pages/detail/detail");
                if($qrcode===false){
                    return $this->renderJson([],'获取失败',ConstantService::$response_fail);
                }
                $active["qrcode"] = $qrcode;
                $active->save();
                $cdnUrl = GlobalUrlService::buildCdnUrl($qrcode);
                return $this->renderJson(['cdnUrl'=>$cdnUrl],"获取成功",ConstantService::$response_success);
            }
            $path = $active["qrcode"];
            $cdnUrl = GlobalUrlService::buildCdnUrl("$path");
            return $this->renderJson(['cdnUrl'=>$cdnUrl],"获取成功",ConstantService::$response_success);

        }
    }
    /**
     * 设置类别
     */
    public function actionSetType(){
        if($this->isAjax() && $this->isPost()){

            $id = $this->post('id');
            $type = $this->post('type');
            $active = Active::findOne(['id'=>$id]);
            $active->type = $type;
            if($active->save()){
                return $this->renderJson([],'设置成功',ConstantService::$response_success);
            }
            return $this->renderJson([],'设置失败,请稍后再试!',ConstantService::$response_fail);

        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);

    }

    public function actionSetTop(){
        if($this->isAjax() && $this->isPost()){

            $id = $this->post('id');
            $set_top = $this->post('set_top');
            $active = Active::findOne(['id'=>$id]);
            $active->set_top = $set_top;
            if($active->save()){
                return $this->renderJson([],'设置成功',ConstantService::$response_success);
            }
            return $this->renderJson([],'设置失败,请稍后再试!',ConstantService::$response_fail);

        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);
    }

}


